//
//  CompanyController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CompanyController.h"

@interface CompanyController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) NSString *companyStr;
@property (nonatomic, strong) NSMutableArray *companyArr;
@property (nonatomic, strong) UITableView *companyTableView;
@property (nonatomic, strong) NSArray *tempArr;
// 添加一个输入框
@property (nonatomic, strong) UITextField *companyText;
@property (nonatomic, strong) UIButton *search;
@end

@implementation CompanyController
- (instancetype)init
{
    self = [super init];
    if (self) {
         self.companyArr = [NSMutableArray array];
        self.tempArr = [NSArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.companyStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ExpressCompany" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
    // Do any additional setup after loading the view.
    self.tempArr = [self.companyStr componentsSeparatedByString:@"\n"];
    for (NSString *tempStr in self.tempArr) {
        // 把文本中的字符串处理，只显示快递公司名称
        NSArray *tempArr = [NSArray array];
        tempArr = [tempStr componentsSeparatedByString:@"=>"];
        NSString *tempCity = [tempArr lastObject];
        NSRange range = {2, tempCity.length - 4};
        tempCity = [tempCity substringWithRange:range];
        NSString *tempMark = [tempArr firstObject];
        NSRange range1 = {3, tempMark.length - 5};
        tempMark = [tempMark substringWithRange:range1];
        NSArray *array = [NSArray arrayWithObjects:tempMark, tempCity, nil];
        [self.companyArr addObject:array];
    }
    // 创建一个tableview来显示每一个公司
    self.companyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 104) style:UITableViewStylePlain];
    [self.view addSubview:self.companyTableView];
    self.companyTableView.delegate = self;
    self.companyTableView.dataSource = self;

    // 添加输入框
    self.companyText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 4 * 3, 40)];
    self.companyText.placeholder = @"请输入快递公司";
    self.companyText.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.companyText];
    self.companyText.delegate = self;
    // 添加搜索按钮
    self.search = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.search setTitle:@"搜索" forState:UIControlStateNormal];
    self.search.frame = CGRectMake(WIDTH / 4 * 3, 0, WIDTH / 4, 40);
    [self.view addSubview:self.search];
    [self.search addTarget:self action:@selector(targetSearch:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cancle.png"] style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
}
- (void)targetCancle:(UIButton *)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)targetSearch:(UIButton *)search
{
    [self.companyText resignFirstResponder];
    NSArray *reduction = [NSArray arrayWithArray:self.companyArr];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSArray *array in self.companyArr) {
        if ([[array lastObject] hasPrefix:self.companyText.text]) {
            [tempArr addObject:array];
        }
    }
    self.companyArr = tempArr;
    [self.companyTableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.companyArr = [NSMutableArray arrayWithArray:reduction];
    });
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.companyArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"0502";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    // 把文本中的字符串处理，只显示快递公司名称
//    NSString *tempStr = [self.companyArr objectAtIndex:indexPath.row];
//    NSArray *tempArr = [NSArray array];
//    tempArr = [tempStr componentsSeparatedByString:@"=>"];
//    NSString *tempStr1 = [tempArr lastObject];
//    NSRange range = {2, tempStr1.length - 4};
//    tempStr1 = [tempStr1 substringWithRange:range];
    
    cell.textLabel.text = [[self.companyArr objectAtIndex:indexPath.row] lastObject];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSString *tempStr = [self.tempArr objectAtIndex:indexPath.row];
//    NSArray *tempArr = [NSArray arrayWithArray:[self.companyArr objectAtIndex:indexPath.row]];
    
//    tempArr = [tempStr componentsSeparatedByString:@"=>"];
//    tempStr = [tempArr firstObject];
//    NSRange range = {3, tempStr.length - 5};
//    NSString *tempStr1 = [tempArr lastObject];
//    NSRange range1 = {2, tempStr1.length - 4};
//    tempStr1 = [tempStr1 substringWithRange:range1];
    
//    tempStr = [tempStr substringWithRange:range];
//    NSLog(@"%@", tempStr);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *str = cell.textLabel.text;
    NSString *mark = [NSString string];
    for (NSArray *tempArr in self.companyArr) {
        if ([[tempArr lastObject] isEqualToString:str]) {
            mark = [tempArr firstObject];
        }
    }
    self.select(mark, str);
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.companyText resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
